from ant import Ant 
import os
import urllib2,urllib
import supplyingant
from paintingant import painter
import threading,Queue,random,time,string
if True:
	genre=list()
	genre.append([u"blues"])
	genre.append([u"classic rock",u"classic-rock"])
	genre.append([u"progressive rock",u"progressiverock",u"progressive-rock"])
	genre.append([u"country"])
	genre.append([u"dance",u"eurodance"])
	genre.append([u"accoustic"])
	genre.append([u"funk"])
	genre.append([u"grunge"])
	genre.append([u"hip hop",u"hip-hop",u"hiphop"])
	genre.append([u"jazz",u"acid jazz"])
	genre.append([u"metal",u"death metal",u"nu metal",u"power metal",u"speed metal"])
	genre.append([u"new age",u"new-age"])
	genre.append([u"oldies"])
	genre.append([u"pop"])
	genre.append([u"r&b",u"rnb"])
	genre.append([u"rap"])
	genre.append([u"reggae"])
	genre.append([u"rock"])
	genre.append([u"techno",u"euro techno",u"euro - techno"])
	genre.append([u"industrial"])
	genre.append([u"alternative"])
	genre.append([u"ska"])
	genre.append([u"biesiada"])
	genre.append([u"soundtrack",u"movie soundtrack",u"movie-soundtrack"])
	genre.append([u"ambient"])
	genre.append([u"trip-hop"])
	genre.append([u"vocal"])
	genre.append([u"electro",u"electro house"])
	genre.append([u"fusion"])
	genre.append([u"trance"])
	genre.append([u"classical",u"classic"])
	genre.append([u"instrumental"])
	genre.append([u"acid"])
	genre.append([u"house"])
	genre.append([u"game"])
	genre.append([u"sound clip"])
	genre.append([u"gospel"])
	genre.append([u"noise"])
	genre.append([u"alternative",u"alternrock",u"alternative Rock",u"alternative-rock",u"alternativerock"])
	genre.append([u"bass",u"drumnbase",u"drumandbase",u"drum and base"])
	genre.append([u"soul"])
	genre.append([u"punk",u"punk rock"])
	genre.append([u"space"])
	genre.append([u"pop-punk",u"pop punk"])
	genre.append([u"instrumental pop"])
	genre.append([u"instrumental rock"])
	genre.append([u"ethnic"])
	genre.append([u"gothic"])
	genre.append([u"hardcore"])
	genre.append([u"techno-industrial"])
	genre.append([u"electronic"])
	genre.append([u"folk",u"pop-folk"])
	genre.append([u"dream"])
	genre.append([u"southern rock"])
	genre.append([u"comedy"])
	genre.append([u"cult"])
	genre.append([u"gangsta"])
	genre.append([u"party music",u"top 40"])
	genre.append([u"christian rap"])
	genre.append([u"pop/funk"])
	genre.append([u"jungle"])
	genre.append([u"native american"])
	genre.append([u"accoustic"])
	genre.append([u"new wave"])
	genre.append([u"psychadelic"])
	genre.append([u"rave"])
	genre.append([u"showtunes"])
	genre.append([u"trailer"])
	genre.append([u"emo"])
	genre.append([u"tribal"])
	genre.append([u"acid punk"])
	genre.append([u"indie",u"indie rock"])
	genre.append([u"retro"])
	genre.append([u"musical"])
	genre.append([u"rock & roll",u"rocknroll",u"rock&roll"])
	genre.append([u"hard rock",u"hardrock"])#############################worker##################################

class parser(threading.Thread,Ant):
	pile = Queue.Queue(5)
	lock=threading.Lock()
	def run(self):
		while True:
			twig = parser.pile.get()####twig = [path,page2parse]
			#print("parser")
			Ant.leave("parser")
			temp=self.fetch(twig)
			if(temp!=""):
				painter.pile.put(temp)
			parser.pile.task_done()
			Ant.comehome("parser")
	def fetch(self,pt):
		if("g" in pt['command_string'] and "a" not in pt['command_string']):
                        self.parse_genre(pt)
                        if (pt['genre_tag']==""):
                                supplyingant.supplier.fail(pt['artist_tag'])
                                return ""
                        return pt
		if("a" in pt['command_string']):
                        self.parse_track_info(pt)
			return pt

	def parse_genre(self,pt):
		pointer=0
		tags=list()
                for i in range(10):
                        tag=self.getfield("name",pt['genre_page'],pointer)
                        size=self.getfield("count",pt['genre_page'],pointer)
                        if(size!="" and tag!=""):
                                try:
                                        if(int(size[0])>75):
                                                tags.append(tag[0])
                                                #print tag[0]

                                except ValueError,msg:
                                        derp=str(msg)[str(msg).find("ValueError"):]
                                        continue
                                pointer=size[1]
                pt['genre_tag']=self.makeid3(tags)
                if (pt['genre_tag']==""):
                        pointer=0
                        tags=list()
                        for i in range(10):
                                tag=self.getfield("name",pt['genre_page'],pointer)
                                size=self.getfield("count",pt['genre_page'],pointer)
                                if(size!="" and tag!=""):
                                        try:
                                                tags.append(tag[0])
                                               # print tag[0]

                                        except ValueError,msg:
                                                derp=str(msg)[str(msg).find("ValueError"):]
                                                continue
                                        pointer=size[1]
                        pt['genre_tag']=self.makeid3(tags,1)
                if(pt['genre_tag']==""):
                        pointer=0
                        with open('failedgenres.txt','a') as f:
                                tags=list()
                                for i in range(10):
                                        tag=self.getfield("name",pt['genre_page'],pointer)
                                        size=self.getfield("count",pt['genre_page'],pointer)
                                        if(size!="" and tag!=""):
                                                try:
                                                        if("<" not in tag[0]):
                                                                f.write(tag[0]+"\n")
                                                       # print tag[0]

                                                except ValueError,msg:
                                                        derp=str(msg)[str(msg).find("ValueError"):]
                                                        continue
                                                pointer=size[1]                        
	def parse_track_info(self,pt):
		pointer=pt['info_page'].find("<album")
		
                if("atag_present"not in pt and pointer!=-1):
                        pt['album_tag']=unicode(self.getfield("title",pt['info_page'],pointer)[0],'utf-8')
                        if("utf-8" in pt['album_tag']):
                                pt['album_tag']=""
		sizelist=['mega','extralarge','large']
		for item in sizelist:
                        album_url=self.getfield("image size=\""+item+"\"",pt['info_page'])
                        if(album_url!="" and "noimage" not in album_url[0] and "utf-8" not in album_url[0]):
                                pt['album_url']=album_url[0]
                                break
                        
		if('album_url' not in pt):
                        pt['album_url']=""
                        pt['album_art']=""
		else:
                        try:pt['album_art']=urllib2.urlopen(pt['album_url']).read()
                        except urllib2.HTTPError:
                                print("403 Forbidden")
                                pt['album_art']=""

		pointer=pt['info_page'].find("<artist")

		pt['corrected_artist_tag']=self.getfield("name",pt['info_page'],pointer)[0]
		if("utf-8" in pt['corrected_artist_tag']): pt['corrected_artist_tag']=""

		#if(pt['corrected_artist_tag']!=pt['artist_tag']):
			#parser.lock.acquire()
			#with open('log.txt','a') as f:
			#	f.write(pt['corrected_artist_tag'].decode('latin-1')+"        "+pt['artist_tag'].decode('latin-1')+"\n")
			#parser.lock.release()
##		pointer=pt['info_page'].find("toptags")
##		temp_genre_tag_list=list()
##		while True:
##			temp=self.getfield("name",pt['info_page'],pointer)
##			pointer=temp[1]
##			if("encoding" in temp[0]): break
##			temp_genre_tag_list.append(temp[0])
##		pt['genre_tag']=self.makeid3(temp_genre_tag_list)
##		if(pt['genre_tag']!=""):
##                        pt['genre_tag']=""
                return 1
##################################################Low level methods###########################################
	def getfield(self,fname,query,pointsto=0):
                first=query.find("<"+fname+">",pointsto)+len(fname)+2
		firstwordofname=""
		for letter in fname:
			if(letter==" "):
				break
			
			firstwordofname=firstwordofname+letter
		second=query.find("</"+firstwordofname+">",first)
		if (first==-1 or second==-1):
                        return ""
		return (query[first:second],second)
	def makeid3(self,liste,desperate=0):
		final=list()
		
		for item in liste:
			for j in range(len(genre)):					#Durchsuchen der Genre Liste nach passendem Genre
				if item.lower() in genre[j]:
						tempstring=string.capwords(genre[j][0])		#Kapitalization des genres
						if tempstring not in final:
                                                        final.append(tempstring)
                        if(desperate==1):
                                if (final!=[]):break
		finalstring=""
		for item in final[:2]:
			finalstring=finalstring+item+"\0"
		return finalstring
##############################################################################################################


#~ test=parser()
#~ artistlist=["prince","paramore","red+hot+chili+peppers","gentleman","yellowcard","eric+clapton","iron+maiden","foo+fighters","the+baseballs"]
#~ #for artist in artistlist:
#~ #	print(artist)
#~ test.fetch()
#~ test.parsetrackinfo()	
